const express = require('express')
const router = express.Router()
const db = require('../../db/index')
const passport = require('passport')


// $router get /total/data/usercount
// @desc 返回管理员数量
// @access private
router.get("/usercount", passport.authenticate("jwt", { session: false }), (req, res) => {
  const sql = `SELECT COUNT(*) AS count  FROM user`
  db.query(sql, (err, results) => {
    if (err) {
      return err.message
    }
    if (results) {
      res.send({
        code: 1,
        data: results,
        msg: '查询成功'
      })
    } else {
      res.send({
        code: 0,
        data: 0,
        msg: '查询失败'
      })
    }

  })
})


// $router get /total/data/profilecount
// @desc 返回订单数量
// @access private
router.get("/profilecount", passport.authenticate("jwt", { session: false }), (req, res) => {
  const sql = `SELECT COUNT(*) AS count  FROM profile`
  db.query(sql, (err, results) => {
    if (err) {
      return err.message
    }
    if (results) {
      res.send({
        code: 1,
        data: results,
        msg: '查询成功'
      })
    } else {
      res.send({
        code: 0,
        data: 0,
        msg: '查询失败'
      })
    }

  })
})

// $router get /total/data/income
// @desc 返回收入情况
// @access private
router.get("/income", passport.authenticate("jwt", { session: false }), (req, res) => {
  const sql = `SELECT SUM(DISTINCT expend) AS expend FROM profile`
  db.query(sql, (err, results) => {
    if (err) {
      return err.message
    }
    if (results) {
      res.send({
        code: 1,
        data: results,
        msg: '查询成功'
      })
    } else {
      res.send({
        code: 0,
        data: 0,
        msg: '查询失败'
      })
    }

  })
})


// $router get /total/data/part
// @desc 返回管理员成份
// @access private
router.get("/part", passport.authenticate("jwt", { session: false }), (req, res) => {
  const sql = `SELECT COUNT(*) AS value ,identity AS name FROM user GROUP BY identity;  `
  db.query(sql, (err, results) => {
    if (err) {
      return err.message
    }
    if (results) {
      res.send({
        code: 1,
        data: results,
        msg: '查询成功'
      })
    } else {
      res.send({
        code: 0,
        msg: '查询失败'
      })
    }

  })
})

// $router get /total/data/source
// @desc 返回管理员成份
// @access private
router.get("/source", (req, res) => {
  const sql = `SELECT COUNT(*) AS count ,type AS name FROM profile GROUP BY type;  `
  db.query(sql, (err, results) => {
    if (err) {
      return err.message
    }
    if (results.length != 0) {
      let items = [];
      results.forEach(item => {
        items.push([item.name, item.count])
      })
      console.log(items);
      res.send({
        code: 1,
        data: items,
        msg: '查询成功'
      })
    } else {
      res.send({
        code: 0,
        msg: '查询失败'
      })
    }

  })
})








module.exports = router